package com.screen.screendisplay.service.impl.electric;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.screen.screendisplay.entity.electric.ElectricProdoperHeating;
import com.screen.screendisplay.entity.electric.ElectricProdoperHeating;
import com.screen.screendisplay.entity.electric.ElectricProdoperPowerGeneral;
import com.screen.screendisplay.mapper.electric.ElectricProdoperHeatingMapper;
import com.screen.screendisplay.service.electric.ElectricProdoperHeatingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.screen.screendisplay.vo.electric.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 供热情况详情表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class ElectricProdoperHeatingServiceImpl extends ServiceImpl<ElectricProdoperHeatingMapper, ElectricProdoperHeating> implements ElectricProdoperHeatingService {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 供热计划完成情况
     * @param year
     * @param month
     * @return
     */
    @Override
    public GrPlanCompletionStatus grPlanCompletionStatus(Integer year, Integer month) {
        LambdaQueryWrapper<ElectricProdoperHeating> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ElectricProdoperHeating::getYear, year)
                .eq(ElectricProdoperHeating::getMonth, month);
        List<ElectricProdoperHeating> dataList = this.list(lqw);
        GrPlanCompletionStatus status = new GrPlanCompletionStatus();
        status.setYear(year);
        status.setMonth(month);
        status.setMonthlyPlanHeating(BigDecimal.valueOf(
                dataList.stream().mapToDouble(item ->
                        item.getMonthlyActualHeating().doubleValue()).sum()));
        status.setMonthlyActualHeating(BigDecimal.valueOf(
                dataList.stream().mapToDouble(item ->
                        item.getMonthlyActualHeating().doubleValue()).sum()
        ));
        status.setMonthlyOverShortage(BigDecimal.valueOf(
                dataList.stream().mapToDouble(item ->
                        item.getMonthlyOverShortage().doubleValue()).sum()
        ));
        status.setMonthlyCompletionRate(BigDecimal.valueOf(
                dataList.stream().mapToDouble(item ->
                        item.getMonthlyCompletionRate().doubleValue()).sum()
        ));
        return status;
    }

    /**
     * 供热量完成趋势
     * @param year
     * @return
     */
    @Override
    public List<GrComplateTrend> grComplateTrend(Integer year) {
        List<GrComplateTrend> result = new ArrayList<>();

        // 查询当前年和去年的数据
        Integer lastYear = year - 1;

        // 构建去年的数据
        GrComplateTrend lastYearTrend = new GrComplateTrend();
        lastYearTrend.setYear(lastYear);
        setMonthlyDataFromDb(lastYearTrend, lastYear);
        result.add(lastYearTrend);

        // 构建今年的数据
        GrComplateTrend currYearTrend = new GrComplateTrend();
        currYearTrend.setYear(year);
        setMonthlyDataFromDb(currYearTrend, year);
        result.add(currYearTrend);

        return result;
    }

    /**
     * 从数据库查询并设置月度数据
     * @param trend ComplateTrend对象
     * @param year 年份
     */
    private void setMonthlyDataFromDb(GrComplateTrend trend, Integer year) {
        String sql = "SELECT month, " +
                "SUM(monthly_actual_heating) as total_actual_power " +
                "FROM qt_dp_electric_prodoper_heating " +
                "WHERE year = ? " +
                "GROUP BY month " +
                "ORDER BY month";

        List<Map<String, Object>> results = jdbcTemplate.queryForList(sql, year);

        for (Map<String, Object> row : results) {
            Integer month = (Integer) row.get("month");
            BigDecimal totalActualPower = (BigDecimal) row.get("total_actual_power");

            switch (month) {
                case 1:
                    trend.setMth_1(totalActualPower);
                    break;
                case 2:
                    trend.setMth_2(totalActualPower);
                    break;
                case 3:
                    trend.setMth_3(totalActualPower);
                    break;
                case 4:
                    trend.setMth_4(totalActualPower);
                    break;
                case 5:
                    trend.setMth_5(totalActualPower);
                    break;
                case 6:
                    trend.setMth_6(totalActualPower);
                    break;
                case 7:
                    trend.setMth_7(totalActualPower);
                    break;
                case 8:
                    trend.setMth_8(totalActualPower);
                    break;
                case 9:
                    trend.setMth_9(totalActualPower);
                    break;
                case 10:
                    trend.setMth_10(totalActualPower);
                    break;
                case 11:
                    trend.setMth_11(totalActualPower);
                    break;
                case 12:
                    trend.setMth_12(totalActualPower);
                    break;
            }
        }
    }

    /**
     * 各个电厂供热完成情况
     * @param year 年份
     * @return
     */
    @Override
    public List<GrPowerPlantSupplyComplateStatus> grPowerPlantSupplyComplateStatus(Integer year) {
        String sql = "SELECT " +
                "power_plant_code, " +
                "power_plant_name, " +
                "MAX(annual_plan_heating) as annual_plan_heating, " +
                "MAX(annual_actual_heating) as annual_actual_heating " +
                "FROM qt_dp_electric_prodoper_heating " +
                "WHERE year = ? " +
                "GROUP BY power_plant_code, power_plant_name " +
                "ORDER BY power_plant_code";

        List<Map<String, Object>> results = jdbcTemplate.queryForList(sql, year);

        List<GrPowerPlantSupplyComplateStatus> result = new ArrayList<>();
        for (Map<String, Object> row : results) {
            GrPowerPlantSupplyComplateStatus status = new GrPowerPlantSupplyComplateStatus();
            status.setYear(year);
            status.setPowerPlantCode((String) row.get("power_plant_code"));
            status.setPowerPlantName((String) row.get("power_plant_name"));
            status.setAnnualPlanHeating((BigDecimal) row.get("annual_plan_heating"));
            status.setAnnualActualHeating((BigDecimal) row.get("annual_actual_heating"));
            result.add(status);
        }
        return result;
    }

}
